package com.ls.lserp.modules.material.service;


import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.common.entity.OgnizationEntity;
import com.ls.lserp.modules.material.dao.SlopworkInRequestDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.SlopworkInRequest;

/**
 * @ClassName: SlopworkInRequestService
 * @Description: 物料入库申请
 * 
 * @author 
 * @date 2017年04月14日
 *
 */

@Service
@Transactional(readOnly = false)
public class SlopworkInRequestService extends CrudService<SlopworkInRequestDao,SlopworkInRequest>{

    @Autowired
    private SlopworkInRequestDao slopworkInRequestDao;
    
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<SlopworkInRequest> findList(Page<SlopworkInRequest> page, SlopworkInRequest slopworkInRequest) {
		
		List<SlopworkInRequest> irDetailList = null;
		BigDecimal tempRequestQuantity = null;
		BigDecimal tempRealQuantity = null;
		BigDecimal tempResidueQuantity = null;
		
		// tab页分，检索条件设定(0:全部,1:待入库,2:未全部入库,3:全部入库)
		slopworkInRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
		
		// 入出库区分（入库)
		slopworkInRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
		
		// 设置分页参数
		slopworkInRequest.setPage(page);
		List<SlopworkInRequest> resultList = slopworkInRequestDao.findList(slopworkInRequest);
		
		if(!CollectionsUtils.isEmpty(resultList)){
			// 库存申请信息
			for(SlopworkInRequest ir : resultList){
				tempRequestQuantity = new BigDecimal("0");
				tempRealQuantity = new BigDecimal("0");
				tempResidueQuantity = new BigDecimal("0");
				irDetailList = slopworkInRequestDao.findClothesRequestDetailList(ir);
				if(!CollectionsUtils.isEmpty(irDetailList)){
					// 库存申请明细
					for(SlopworkInRequest ird : irDetailList){
						tempRequestQuantity = tempRequestQuantity.add(checkBigDecimalNull(ird.getRequestQuantity()));
						tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealQuantity()));
					}
					tempResidueQuantity = tempRequestQuantity.subtract(tempRealQuantity);
					ir.setRequestQuantity(tempRequestQuantity);
					ir.setRealQuantity(tempRealQuantity);
					ir.setResidueQuantity(tempResidueQuantity);
				}
			}
		}
		
        // 执行分页查询
        page.setList(resultList);
        
        return page;
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
    /**
     * @Title: getProductionList
     * @Description: 查询
     * @param @return
     * @return List<SlopworkInRequest> 
     * @throws
     */
   public List<SlopworkInRequest> getProductionList() {
       
       return slopworkInRequestDao.getProductionList();
   }
   
   /**
    * @Title: getFabricatList
    * @Description: 查询
    * @param @return
    * @return List<ognization> 
    * @throws
    */
  public List<OgnizationEntity> getFabricatList() {
      
      return slopworkInRequestDao.getFabricatList();
  }
  
  /**
   * @Title: getContractProcessingList
   * @Description: 查询
   * @param @param slopworkInRequest
   * @param @return
   * @return List<SlopworkInRequest> 
   * @throws
   */
 public List<SlopworkInRequest> getContractProcessingList(SlopworkInRequest slopworkInRequest) {
     
     return slopworkInRequestDao.getContractProcessingList(slopworkInRequest);
 }
 
 /**
  * @Title: getStyleList
  * @Description: 查询
  * @param @param slopworkInRequest
  * @param @return
  * @return List<ognization> 
  * @throws
  */
public List<SlopworkInRequest> getStyleList(SlopworkInRequest slopworkInRequest) {
    
    return slopworkInRequestDao.getStyleList(slopworkInRequest);
}

/**
 * @Title: getProduction
 * @Description: 查询
 * @param @param getProduction
 * @param @return
 * @return SlopworkInRequest
 * @throws
 */
public SlopworkInRequest getProduction(SlopworkInRequest slopworkInRequest) {
   
   return slopworkInRequestDao.getProduction(slopworkInRequest);
}

/**
 * @Title: getContractNo
 * @Description: 查询客户款号和客户合同号
 * @param @param slopworkInRequest
 * @param @return
 * @return SlopworkInRequest
 * @throws
 */
public SlopworkInRequest getContractNo(SlopworkInRequest slopworkInRequest) {
   
   return slopworkInRequestDao.getContractNo(slopworkInRequest);
}

/**
 * @Title: findCreateRequestDetailList
 * @Description: 入库申请明细
 * @param @param slopworkInRequest
 * @param @return
 * @return List<SlopworkInRequest>
 * @throws
 */
public List<SlopworkInRequest> findCreateRequestDetailList(SlopworkInRequest slopworkInRequest) {
   
   return slopworkInRequestDao.findCreateRequestDetailList(slopworkInRequest);
}
/**
 * @Title: saveInventoryRequest
 * @Description: 保存入库申请
 * @param @param inventoryRequest
 * @return void 
 * @throws
 */
@Transactional(readOnly = false)
public void saveInventoryRequest(SlopworkInRequest slopworkInRequest) {
	
	// 判断新建编辑
	//若为null 执行插入语句（新建）
	if (slopworkInRequest.getId()==null){
        if(slopworkInRequest.getStatusFlg() !=null){
			//获取样品入库的styleId
			slopworkInRequest.setStyleId(slopworkInRequest.getStyleIdold());
		}
		slopworkInRequest.preInsert();
		slopworkInRequest.setVersion(1L);
		slopworkInRequest.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
		slopworkInRequest.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
		slopworkInRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
		slopworkInRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
		slopworkInRequestDao.insertInventoryRequest(slopworkInRequest);
	}else{
		// 更新数据
		slopworkInRequest.preUpdate();
        if(slopworkInRequest.getStatusFlg() !=null){
            //获取样品入库的styleId
            slopworkInRequest.setStyleId(slopworkInRequest.getStyleIdold());
        }
        //样品编辑时，实际上该数据并没有更新
		slopworkInRequestDao.updateInventoryRequest(slopworkInRequest);
		// 删除入库单明细
		slopworkInRequestDao.deleteRequestDetails(slopworkInRequest);
	}
	
	
	// 判断list是否为空
	if(!CollectionsUtils.isEmpty(slopworkInRequest.getRequestDetailsList())){
	   // 遍历赋值
       for(SlopworkInRequest itr : slopworkInRequest.getRequestDetailsList()){
      	 itr.setId(slopworkInRequest.getId());
      	 itr.preInsert();
      	 itr.setVersion(1L);
      	 itr.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
      	 itr.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
      	 itr.setStockinStatus(ResourcesUtil.getConfig("inventory_status_wait"));
		   if(itr.getRequestQuantity()!=null &&itr.getRequestQuantity().intValue() > 0){
			   slopworkInRequestDao.insertInventoryRequestDetails(itr);
		   }

		}
	}
}

/**
 * @Title: findShowRequestDetailList
 * @Description: 入库申请明细
 * @param @param slopworkInRequest
 * @param @return
 * @return List<SlopworkInRequest>
 * @throws
 */
public List<SlopworkInRequest> findShowRequestDetailList(SlopworkInRequest slopworkInRequest) {
   
   return slopworkInRequestDao.findShowRequestDetailList(slopworkInRequest);
}
	/**
	 * @Title: getProductionList
	 * @Description: 查询
	 * @param @return
	 * @return List<SlopworkInRequest>
	 * @throws
	 */
	public List<SlopworkInRequest>  getSamplyData(Long id) {

		return slopworkInRequestDao.getSamplyData(id);
	}
    /**
     * @Title: getProductionList
     * @Description: 查询
     * @param @return
     * @return List<SlopworkInRequest>
     * @throws
     */
    public List<SlopworkInRequest>  getSamplyDataCreate(Long id) {

        return slopworkInRequestDao.getSamplyDataCreate(id);
    }
	/**
	 * @Title: findSamplyShowRequestDetailList
	 * @Description: 样品入库申请明细
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return List<SlopworkInRequest>
	 * @throws
	 */
	public List<SlopworkInRequest> findSamplyShowRequestDetailList(SlopworkInRequest slopworkInRequest) {

		return slopworkInRequestDao.findSamplyShowRequestDetailList(slopworkInRequest);
	}
}